/**
 * 作者: hwp
 * 日期: 2025-05-14 09:17:46
 * 备注: 用户联盟宝箱
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserUnionBox;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserUnionBoxModel extends BaseModel implements Model {
    public static final String LISTKEY = "";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // 用户id
    protected int receiveBoxId; // 当前的宝箱ID
    protected int receiveBoxNum; // 领取的宝箱进度的周期 (进入联盟时从当前期开始领取，离开时补发未领取的，并重置成0)
    protected int receiveActiveBoxNum; // 领取每日活跃礼盒数量 (活跃礼盒的领取数)
    protected long resetTime; // 重置时间

    public void reset() {
    }

    protected String getKey() {
        return "UserUnionBox." + id;
    }

    protected String getListKey() {
        return null;
    }

    protected String getListItemKey() {
        return null;
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_union_box"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, id, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, id, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入库
     */
    public JsonObject updateNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE_NOW, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 用户id
        obj.put("receiveBoxId", receiveBoxId); // 当前的宝箱ID
        obj.put("receiveBoxNum", receiveBoxNum); // 领取的宝箱进度的周期 (进入联盟时从当前期开始领取，离开时补发未领取的，并重置成0)
        obj.put("receiveActiveBoxNum", receiveActiveBoxNum); // 领取每日活跃礼盒数量 (活跃礼盒的领取数)
        obj.put("resetTime", resetTime); // 重置时间
        return obj;
    }

    public PBUserUnionBox.Builder takeInitBuilder() {
        PBUserUnionBox.Builder sb = PBUserUnionBox.newBuilder();
        if (receiveBoxNum != 0) {
            sb.setReceiveBoxNum(receiveBoxNum);
        }
        if (receiveActiveBoxNum != 0) {
            sb.setReceiveActiveBoxNum(receiveActiveBoxNum);
        }
        return sb;
    }

    public PBUserUnionBox.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserUnionBox.Builder sb = PBUserUnionBox.newBuilder();
        if (isFieldUpdate("receiveBoxNum")) {
            sb.setReceiveBoxNum(receiveBoxNum);
        }
        if (isFieldUpdate("receiveActiveBoxNum")) {
            sb.setReceiveActiveBoxNum(receiveActiveBoxNum);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public int getReceiveBoxId() {
        return receiveBoxId;
    }

    public void putReceiveBoxId(int receiveBoxId) {
        if (this.receiveBoxId != receiveBoxId) {
            this.receiveBoxId = receiveBoxId;
            updateRedisHashField(getKey(), "receiveBoxId", this.receiveBoxId);
        }
    }

    public void addReceiveBoxId(int value) {
        this.receiveBoxId += value;
        updateRedisHashField(getKey(), "receiveBoxId", this.receiveBoxId);
    }

    public int getReceiveBoxNum() {
        return receiveBoxNum;
    }

    public void putReceiveBoxNum(int receiveBoxNum) {
        if (this.receiveBoxNum != receiveBoxNum) {
            this.receiveBoxNum = receiveBoxNum;
            updateRedisHashField(getKey(), "receiveBoxNum", this.receiveBoxNum);
        }
    }

    public void addReceiveBoxNum(int value) {
        this.receiveBoxNum += value;
        updateRedisHashField(getKey(), "receiveBoxNum", this.receiveBoxNum);
    }

    public int getReceiveActiveBoxNum() {
        return receiveActiveBoxNum;
    }

    public void putReceiveActiveBoxNum(int receiveActiveBoxNum) {
        if (this.receiveActiveBoxNum != receiveActiveBoxNum) {
            this.receiveActiveBoxNum = receiveActiveBoxNum;
            updateRedisHashField(getKey(), "receiveActiveBoxNum", this.receiveActiveBoxNum);
        }
    }

    public void addReceiveActiveBoxNum(int value) {
        this.receiveActiveBoxNum += value;
        updateRedisHashField(getKey(), "receiveActiveBoxNum", this.receiveActiveBoxNum);
    }

    public long getResetTime() {
        return resetTime;
    }

    public void putResetTime(long resetTime) {
        if (this.resetTime != resetTime) {
            this.resetTime = resetTime;
            updateRedisHashField(getKey(), "resetTime", this.resetTime);
        }
    }

    public void addResetTime(long value) {
        this.resetTime += value;
        updateRedisHashField(getKey(), "resetTime", this.resetTime);
    }

}